import jieba

txt = open('test.txt', "rb").read()
words = jieba.lcut(txt)
counts = {}
for word in words:
    if len(word) == 1:
        continue
    elif word == "\r\n":
        del(word)
    elif word == "悟空" or word == "行者" or word == "老孙" or word == "大师兄" or word == "大圣":
        rword = "孙悟空"
    elif word == "悟能" or word == "八戒" or word == "老猪" or word == "二师兄":
        rword = "猪八戒"
    elif word == "悟静" or word == "老沙":
        rword = "沙僧"
    elif word == "师傅" or word == "师父" or word == "三藏" or word == "玄奘" or word == "御弟" or word == "唐僧" or word == "长老":
        rword = "唐三藏"
    else:
        rword = word
    counts[rword] = counts.get(rword, 0) + 1
items = list(counts.items())
items.sort(key=lambda x: x[1], reverse=True)
ls = []
for i in range(5):
    word, count = items[i]
    ls.append(word)
    ls.append(str(count))
    print("{0:<10}{1:>5}".format(word, count))
print(ls)
out = open('out.txt', "wb")
out.writelines(ls)
# for line in ls:
#     out.write(line + ",")
#     print(line)
out.close()